import {getToken, setToken, removeToken} from '@/utils/auth'
import router, {permissionMenu} from '@/router'
import api from '@/utils/api'

const getDefaultState = () => {
    return {
        token: getToken(),
        userInfo: {},
        role: undefined,
        router: [],
        editable: false
    }
}

const state = getDefaultState()

const mutations = {
    RESET_STATE: (state) => {
        Object.assign(state, getDefaultState())
    },
    SET_TOKEN: (state, token) => {
        state.token = token
    },
    SET_USER_INFO: (state, userInfo) => {
        state.userInfo = userInfo
    },
    SET_ROLE: (state, menus) => {
        state.role = menus
    },
    SET_ROUTER: (state, router) => {
        state.router = router
    },
    setEditable: (state, status) => {
        state.editable = status;
    }
}

const actions = {
    login({commit}, data) {
        // commit('SET_TOKEN', "res.data")
        // setToken("res.data")
        // if (true) return

        return new Promise((resolve, reject) => {
            let userInfo = JSON.parse(JSON.stringify(data))
            userInfo['type'] = 'ACCOUNT'
            userInfo['accountType'] = 'PLATFORM'
            userInfo['channel'] = 'PC'
            api
                .post('/api/account/login', {}, userInfo)
                .then((res) => {
                    if (res.code === 200) {
                        commit('SET_TOKEN', res.data)
                        setToken(res.data)
                    }
                    resolve()
                })
                .catch((error) => {
                    console.error(error)
                    reject(error)
                })
        })
    },

    getInfo({commit, state}) {

        commit('SET_ROUTER', permissionMenu([]))
        return new Promise((resolve, reject) => {
            api
                .get('/api/account/get_info', {token: state.token})
                .then((res) => {
                    if (res.code === 200) {
                        const routerData = permissionMenu(res.data.role)
                        commit('SET_ROLE', res.data.role)
                        commit('SET_USER_INFO', res.data)
                        commit('SET_ROUTER', routerData)
                        console.log('get_info', routerData)
                        router.addRoutes(routerData)
                        resolve(routerData)
                    } else reject(res)
                })
                .catch((error) => {
                    reject(error)
                })
        })
    },

    logout({commit}) {
        return new Promise((resolve, reject) => {
            api.post('/api/account/logout')
                .then(() => {
                    removeToken() // must remove  token  first
                    commit('SET_TOKEN', undefined)
                    commit('SET_USER_INFO', {})
                    commit('SET_ROUTER', [])
                    resolve()
                })
                .catch((error) => {
                    reject(error)
                })
        })
    },

    showEdit({commit}) {
        commit('setEditable', true)
    },
    hideEdit({commit}) {
        commit('setEditable', false)
    }

}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
